{include file="public/header" /}
<body>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <div class="layui-input-inline">
                        <input type="text" name="start" class="layui-input" onfocus="wk.lay_date(this);" placeholder="创建开始日期">
                    </div>
                    <div class="layui-form-mid">
                        -
                    </div>
                    <div class="layui-input-inline">
                        <input type="text" name="end" class="layui-input" onfocus="wk.lay_date(this);" placeholder="创建结束日期">
                    </div>
                </div>
                <div class="layui-inline">
                    <div class="layui-input-inline">
                        <input type="text" id="key" class="layui-input" name="key" value="" placeholder="输入需查询的角色名"/>
                    </div>
                </div>
                <div class="layui-inline">
                    <div class="layui-input-inline">
                        <button class="layui-btn" lay-submit="" lay-filter="LAY-search">立即搜索</button>
                        <button  class="layui-btn layui-btn-normal" id="empty"  lay-submit="" lay-filter="LAY-search">重置</button>
                    </div>
                </div>
            </div>
            <div>
                <button class="layui-btn" data-type="add" onclick="wk.layer_show('添加角色','{:url('roleAdd')}')">
                    <i class="fa fa-plus"></i> 添加角色
                </button>
                <span class="layui-btn-dropdown" style="display:inline-block;">
                    <button class="layui-btn layui-btn-danger" data-toggle="dropdown"><i class="fa fa-wrench"></i> 批量操作 <i class="fa fa-caret-down"></i></button>
                    <ul class="layui-dropdown-menu layui-anim layui-anim-upbit">
                        <li><a href="javascript:;" class="layuiBtn" data-type="getCheckData"><i class="fa fa-trash-o"></i> 批量删除 </a></li>
                            <li><a  href="javascript:;" class="layuiBtn" data-type="usingAll" id="onAll"><i class="fa fa-check-circle"></i> 批量启用</a></li>
                            <li><a  href="javascript:;" class="layuiBtn" data-type="forbiddenAll" id="offAll"><i class="fa fa-ban"></i> 批量禁用</a></li>
                    </ul>
                </span>
            </div>
        </div>

        <div class="layui-card-body">
            <table id="LAY-table-manage" lay-filter="LAY-table-manage"></table>
            <!--操作模板-->
            <script type="text/html" id="opeBar">
                <a class="layui-btn layui-btn-xs" title="编辑" onclick="wk.layer_show('编辑角色','{:url('roleEdit')}?id={{d.id}}')"><i class="fa fa-pencil"></i></a>
                {{# if(d.id!==1){ }}
                    <a class="layui-btn layui-btn-info layui-btn-xs" title="权限分配" onclick="giveQx({{d.id}})"><i class="fa fa-unlock"></i></a>
                    <a class="layui-btn layui-btn-danger layui-btn-xs" title="删除" onclick="wk.confirm('{{d.id}}','{:url('roleDel')}')"><i class="fa fa-trash-o"></i></a>
                {{# } }}
            </script>
            <!--状态模板-->
            <script type="text/html" id="staBar">
                {{# if(d.id!==1){ }}
                    <input type="checkbox" value="{{d.id}}"  lay-skin="switch"  lay-text="ON|OFF" lay-filter="OnOff"{{ d.status == 1 ? 'checked' : '' }} >
                {{# } }}
            </script>
        </div>
    </div>
</div>
<!-- 权限分配 -->
<div class="zTreeDemoBackground left" style="display: none" id="role">
    <input type="hidden" id="nodeid">
    <div class="layui-form-item layui-col-xs-offset3 layui-col-xs5">
        <ul id="treeType" class="ztree"></ul>
    </div>
    <div class="layui-form-item layui-col-xs-offset3 layui-col-xs2">
        <input value="确认分配" class="layui-btn" id="postform"/>
    </div>
</div>
{include file="public/footer" /}
<script>
    layui.use(['index', 'table'], function () {
        var $ = layui.$
            , form = layui.form
            , table = layui.table
        table.render({
            elem: '#LAY-table-manage'
            , url: '{:url("Role/index")}'
            ,response: {
                statusCode: 220 //成功的状态码，默认：0
            }
            , page: true
            , even: false //开启隔行背景
            , size: 'lg' //sm小尺寸的表格 lg大尺寸
            // ,width:100
            , autoSort: false
            , cellMinWidth: 150
            , height: "full-220"
            , limits: [10, 20, 30, 40, 50]
            , limit: "{:config('pages')}"
            , loading: true
            , id: 'LAY-table'
            , cols: [[
                {type: 'checkbox', fixed: 'left',}
                , {type:'numbers', width: 80, title: '序号'}
                , {field: 'title', width: '', title: '角色名称', align: 'center'}
                , {field: 'describe', width: '', title: '角色描述', align: 'center'}
                , {field: 'create_time', width: '', title: '添加时间', sort: true, align: 'center'}
                , {field: 'update_time', width: '', title: '更新时间', sort: true, align: 'center'}
                , {field: 'status',fixed: 'right', width: 100, title: '状态', align: 'center', templet: '#staBar'}
                , {fixed: 'right', width: 140, title: '操作', align: 'center', toolbar: '#opeBar'}
            ]]
        });
        //监听状态开关操作
        form.on('switch(OnOff)', function (obj) {
            var num = '';
            obj.elem.checked == true? num = 1: num = 2;
            //角色状态
            wk.status(this.value,num, '{:url("role_state")}',obj);
        });
        //事件
        var active = {
            getCheckData: function(){
                //批量删除
                wk.batchDel(getIds(),"{:url('batchDelRole')}");
            }
            ,forbiddenAll: function(){
                //批量禁用
                wk.batchForbidden(getIds(),2,"{:url('forbiddenRole')}");
            }
            ,usingAll: function(){
                //批量启用
                wk.usingAll(getIds(),1,"{:url('usingRole')}");
            }
        };

        $('.layuiBtn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
        var getIds = function () {
            var ids = [];
            var checkStatus = table.checkStatus('LAY-table')
                ,data = checkStatus.data;
            $.each(data,function(index,item){
                ids.push(item['id'])
            });
            return ids;
        }
    });
</script>
<script type="text/javascript">
    zNodes = '';
    var index = '';
    var index2 = '';
    //分配权限
    function giveQx(id){
        $("#nodeid").val(id);
        //加载层
        index2 = layer.load(0, {shade: false}); //0代表加载的风格，支持0-2
        //获取权限信息
        $.getJSON("{:url('giveAccess')}", {'type' : 'get', 'id' : id}, function(res){
            layer.close(index2);
            if(res.code == 200){
                zNodes = JSON.parse(res.data);  //将字符串转换成obj

                //页面层
                var index = wk.layer_show1("权限分配",$('#role'),400);
                //设置位置
                layer.style(index, {
                    top: '5%'
                });

                //设置zetree
                var setting = {
                    view: {
                        dblClickExpand: false,//是否双击展开
                        showLine: true,//设置为显示或隐藏线条
                        selectedMulti: false,//设置是否允许选择多个节点
                        expandSpeed: "slow",//节点展开动画速度  "slow"、 "normal"、"fast"
                        showIcon: true,//设置为显示或隐藏节点图标
                        // fontCss : {color:"red"},//个性化的文本样式
                        dblClickExpand: true, //双击展开父节点的功能
                    },
                    check:{
                        enable:true,//设置为接受简单数据格式
                        chkStyle: "checkbox",//复选框类型
                        chkboxType: { "Y": "ps", "N": "s" }//当一个节点被选中或未选中时，控制其父节点及其子节点自动选中或取消选中p:父节点  s:子节点
                    },
                    data: {
                        simpleData: {
                            enable: true,//将zTree的节点设置为接受简单数据格式
                        }
                    }
                };

                $.fn.zTree.init($("#treeType"), setting, zNodes);
                var zTree = $.fn.zTree.getZTreeObj("treeType");
                zTree.expandAll(true);

            }else if(res.code == 100){
                wk.error(res.msg);
            }
        });
    }

    //确认分配权限
    $("#postform").click(function(){
        var zTree = $.fn.zTree.getZTreeObj("treeType");
        var nodes = zTree.getCheckedNodes(true);
        var NodeString = '';
        $.each(nodes, function (n, value) {
            if(n>0){
                NodeString += ',';
            }
            NodeString += value.id;
        });
        var id = $("#nodeid").val();
        //写入库
        $.post("{:url('giveAccess')}", {'type' : 'give', 'id' : id, 'rule' : NodeString}, function(res){
            layer.closeAll();
            if(res.code == 200){
                wk.success(res.msg);
            }else if(res.code == 100){
                wk.error(res.msg);
            }

        }, 'json')
    })
</script>
</body>
</html>